<?php
/**
 * HomeController.php
 *
 * @author: Cyw
 * @email: chenyunwen01@bianfeng.com
 * @created: 2015/4/16 14:23
 * @logs:
 *
 */
namespace App\Http\Controllers;

use App\Entities\InspiringModel;
use App\Entities\TagsModel;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class HomeController extends Controller
{
    public function index(){
        $tags = TagsModel::all()->lists('name', 'id');

        return view('index', compact('tags'));
    }

    public function getContent(Request $request)
    {
        $tagid = $request->input('tagid');
        $i = InspiringModel::where('tagid', $tagid)->get()->toArray();
        return success($i);
    }

    /**
     * 添加
     *
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function add(Request $request)
    {
        $tags = TagsModel::all()->lists('name', 'id');

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $title = $request->input('title');
            $tag = $request->input('tag');

            if (empty($tag)){
                $tag = '其他';
            }

            $tagid = 0;
            $tagobj = TagsModel::where('name', $tag)->first();
            if (!$tagobj) {
                $tagobj = new TagsModel();
                $tagobj->name = $tag;
                $tagobj->save();
            }
            $tagid = $tagobj->id;

            InspiringModel::create(array(
                'title' => e($title),
                'tagid' => $tagid
            ));
            Session::flash('message', '添加成功');
        }
        return view('add', compact('tags'));
    }

    public function edit($id)
    {

    }

    public function remove()
    {

    }
}